package com.ruoyi.module.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.domain.DictWord;
import com.ruoyi.module.mapper.DictWordMapper;

/**
 * 词典单词Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/module/dict/word")
public class DictWordController extends BaseController
{
    @Autowired
    private DictWordMapper dictWordMapper;

    /**
     * 根据词典ID查询单词列表（公开接口，无需特殊权限）
     * 前端已有词典访问控制（免费词典 vs VIP词典）
     */
    @GetMapping("/book/{bookId}")
    public AjaxResult listByBookId(@PathVariable("bookId") String bookId)
    {
        List<DictWord> list = dictWordMapper.selectDictWordListByBookId(bookId);
        return AjaxResult.success(list);
    }

    /**
     * 根据词典ID查询单词列表（带标签信息）
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:query')")
    @GetMapping("/book/{bookId}/tags")
    public TableDataInfo listWithTagsByBookId(@PathVariable("bookId") String bookId)
    {
        startPage();
        DictWord dictWord = new DictWord();
        dictWord.setBookId(bookId);
        List<DictWord> list = dictWordMapper.selectDictWordListWithTags(dictWord);
        return getDataTable(list);
    }

    /**
     * 修改单词
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:edit')")
    @Log(title = "词典单词", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DictWord dictWord)
    {
        dictWord.setUpdateBy(getUsername());
        return toAjax(dictWordMapper.updateDictWord(dictWord));
    }

    /**
     * 删除单词
     */
    @PreAuthorize("@ss.hasPermi('module:dict:book:remove')")
    @Log(title = "词典单词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(dictWordMapper.deleteDictWordById(id));
    }
}

